package com.aibili.contoller.api;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aibili.service.GiftService;
import com.alipay.trans.config.AlipayConfig;
import com.alipay.trans.util.AlipayNotify;
import com.alipay.trans.util.AlipaySubmit;


@Controller
@RequestMapping("/api/alipaytrans")
public class ApiAlipaytransContoller extends ApiBaseContoller{

	@Resource
	GiftService giftService;
	@ResponseBody
	@RequestMapping(value = "/alipayapi", produces = "application/json; charset=UTF-8")
		public void alipayapi(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//服务器异步通知页面路径
				String notify_url = AlipayConfig.notify_url;
				//需http://格式的完整路径，不允许加?id=123这类自定义参数

				//付款账号
				String email = new String(request.getParameter("WIDemail").getBytes("ISO-8859-1"),"UTF-8");
				//必填

				//付款账户名
				String account_name = new String(request.getParameter("WIDaccount_name").getBytes("ISO-8859-1"),"UTF-8");
				//必填，个人支付宝账号是真实姓名公司支付宝账号是公司名称

				//付款当天日期
				String pay_date = new String(request.getParameter("WIDpay_date").getBytes("ISO-8859-1"),"UTF-8");
				//必填，格式：年[4位]月[2位]日[2位]，如：20100801

				//批次号
				String batch_no = new String(request.getParameter("WIDbatch_no").getBytes("ISO-8859-1"),"UTF-8");
				//必填，格式：当天日期[8位]+序列号[3至16位]，如：201008010000001

				//付款总金额
				String batch_fee = new String(request.getParameter("WIDbatch_fee").getBytes("ISO-8859-1"),"UTF-8");
				//必填，即参数detail_data的值中所有金额的总和

				//付款笔数
				String batch_num = new String(request.getParameter("WIDbatch_num").getBytes("ISO-8859-1"),"UTF-8");
				//必填，即参数detail_data的值中，“|”字符出现的数量加1，最大支持1000笔（即“|”字符出现的数量999个）

				//付款详细数据
				String detail_data = new String(request.getParameter("WIDdetail_data").getBytes("ISO-8859-1"),"UTF-8");
				//必填，格式：流水号1^收款方帐号1^真实姓名^付款金额1^备注说明1|流水号2^收款方帐号2^真实姓名^付款金额2^备注说明2....
				
				
				//////////////////////////////////////////////////////////////////////////////////
				
				//把请求参数打包成数组
				Map<String, String> sParaTemp = new HashMap<String, String>();
				sParaTemp.put("service", "batch_trans_notify");
		        sParaTemp.put("partner", AlipayConfig.partner);
		        sParaTemp.put("_input_charset", AlipayConfig.input_charset);
				sParaTemp.put("notify_url", notify_url);
				sParaTemp.put("email", email);
				sParaTemp.put("account_name", account_name);
				sParaTemp.put("pay_date", pay_date);
				sParaTemp.put("batch_no", batch_no);
				sParaTemp.put("batch_fee", batch_fee);
				sParaTemp.put("batch_num", batch_num);
				sParaTemp.put("detail_data", detail_data);
				
				//建立请求
				String sHtmlText = AlipaySubmit.buildRequest(sParaTemp,"get","确认");
				PrintWriter out = response.getWriter();
				out.println(sHtmlText);
				out.flush();
				out.close();
				
		}
	
	@ResponseBody
	@RequestMapping(value = "/notify", produces = "application/json; charset=UTF-8")
	public void notify2(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//获取支付宝POST过来反馈信息
		Map<String,String> params = new HashMap<String,String>();
		Map requestParams = request.getParameterMap();
		for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i]
						: valueStr + values[i] + ",";
			}
			//乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
			//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
			params.put(name, valueStr);
		}
		
		//获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表(以下仅供参考)//
		//批量付款数据中转账成功的详细信息

		String success_details = new String(request.getParameter("success_details").getBytes("ISO-8859-1"),"UTF-8");

		//批量付款数据中转账失败的详细信息
		String fail_details = new String(request.getParameter("fail_details").getBytes("ISO-8859-1"),"UTF-8");

		String batch_no = new String(request.getParameter("batch_no").getBytes("ISO-8859-1"),"UTF-8");
		//获取支付宝的通知返回参数，可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//

		if(AlipayNotify.verify(params)){//验证成功
			//////////////////////////////////////////////////////////////////////////////////////////
			//请在这里加上商户的业务逻辑程序代码
			giftService.changep(batch_no);
			//——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
			
			//判断是否在商户网站中已经做过了这次通知返回的处理
				//如果没有做过处理，那么执行商户的业务程序
				//如果有做过处理，那么不执行商户的业务程序
			
			PrintWriter out = response.getWriter();	
			out.println("success");	//请不要修改或删除
			out.flush();
			out.close();
			
			//——请根据您的业务逻辑来编写程序（以上代码仅作参考）——

			//////////////////////////////////////////////////////////////////////////////////////////
		}else{//验证失败
			PrintWriter out = response.getWriter();
			out.println("fail");
			out.flush();
			out.close();
		}
	}
}
